﻿{
 extUtils
 --------
 Additional various routine which not yet available and/or not yet fixed on Lazarus v1.0.x
 
 This Unit is Part of GauExtUtils Package Component for Lazarus.
 Code name Package		: H-Băng
 Branch name Package	: Anh Giờ Đây
 Last Updated			: 12:14 AM 04/02/2013 (GMT+07)

 Copyright (c) 2012 - 2013, Takeda Matsuki and others
 License : Modified LGPL (follow Lazarus license). 
}

unit extSysUtils;

{$mode objfpc}{$H+}

{$IF FPC_FULLVERSION >= 20700}
 {$CodePage UTF8}
{$ENDIF}

interface

uses
 Classes, SysUtils;
 
  
function extBoolToInt(B:Boolean):Integer;
function extIntToBool(I:Integer):Boolean;

function extWordToStr(wrd: word): string;
function extTime2Str(timeInMs: Int64; fmt: string = 'hh:mm:ss.ms'): string;
  
implementation

function extBoolToInt(B:Boolean):Integer;
begin
 Result:= Ord(B);
end;

function extIntToBool(I:Integer):Boolean;
begin
 Result:=I<>0;
end;


function extWordToStr(wrd: word): string;
begin
  if (wrd < 10)  then 
   Result := '0' + IntToStr(wrd)
  else              
   Result := IntToStr(wrd);
end;

function extTime2Str(timeInMs: Int64; fmt: string = 'hh:mm:ss.ms'): string;
var
  dd, hh, mm, ss, ms: word;
begin
  ms := timeInMs mod 1000; 
  timeInMs := timeInMs div 1000;
  ss := timeInMs mod 60;   
  timeInMs := timeInMs div 60;
  mm := timeInMs mod 60;   
  timeInMs := timeInMs div 60;
  hh := timeInMs mod 24;   
  timeInMs := timeInMs div 24;
  dd := timeInMs;

  Result := fmt;
  Result := StringReplace(Result, 'dd',  extWordToStr(dd), [rfReplaceAll, rfIgnoreCase]);
  Result := StringReplace(Result, 'hh',  extWordToStr(hh), [rfReplaceAll, rfIgnoreCase]);
  Result := StringReplace(Result, 'mm',  extWordToStr(mm), [rfReplaceAll, rfIgnoreCase]);
  Result := StringReplace(Result, 'ss',  extWordToStr(ss), [rfReplaceAll, rfIgnoreCase]);
  Result := StringReplace(Result, 'ms',  extWordToStr(ms), [rfReplaceAll, rfIgnoreCase]);
end;  

end.